package com.hbnu.controller;

import com.alibaba.fastjson.JSONObject;
import com.hbnu.domain.Books;
import com.hbnu.domain.UserInfo;
import com.hbnu.service.BooksService;
import com.hbnu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
public class BooksController {
    @Autowired
    private BooksService booksService;

    @Autowired
    private UserService userService;

    //查找所有书籍功能
    public void searchAllBooks(){
        List<Books> bookList = booksService.findAllBooks();

        for (Books book : bookList) {
            System.out.println(book.toString());//打印书籍信息
        }
    }
    //查找单本书籍功能
    public void searchBook(int bid){
        Books book = booksService.findBook(bid);
        book.toString();
    }
    //查询书名功能
    public String searchbName(int bid){
        String bname = booksService.findbName(bid);

        return bname;
    }
    //借书功能
    public void borrowBook(int bid){
        int row = booksService.lendBook(bid);
        System.out.println("«"+searchbName(bid)+"» "+"借阅成功！！借阅后图书信息如下：");
        searchAllBooks();
    }
    //还书功能
    public void backBook(int bid){
        booksService.returnBook(bid);
        System.out.println("«"+searchbName(bid)+"» "+"成功还入！！还入后图书信息如下：");
        searchAllBooks();
    }
    //添加书籍功能
    public void newBook(Books book){
        booksService.addBook(book);
        System.out.println("已将"+" «"+book.getBname()+"» "+"添加入库！！以下是添加后的图书数据：");
        searchAllBooks();
    }
    //修改书籍功能
    public void modifyBook(Books book){
        booksService.updateBook(book);
        System.out.println("«"+book.getBid()+"» "+"号书籍信息已更新！！以下是更新后的图书信息：");
        searchAllBooks();
    }
    //删除书籍功能
    public void cutBook(int bid){
        String bname = searchbName(bid);
        booksService.deleteBook(bid);
        System.out.println("书籍"+" «"+bname+"» "+"已从库中删除，变更后的图书信息如下：");
        searchAllBooks();
    }
}
